﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SmartRoute.Data
{
    [Serializable]
    public class School: ISerializable
    {
        //Where the school is

        Location _address;

        public Location Address
        {
            get { return _address; }
            set { _address = value; }
        }

        //What the school is called

        string _schoolName;

        public string SchoolName
        {
            get { return _schoolName; }
            set { _schoolName = value; }
        }

        //Bell times?

        //TimeSpan _startingTimeOfDay;

        //public TimeSpan OpeningTime
        //{
        //    get { return _startingTimeOfDay; }
        //    set { _startingTimeOfDay = value; }
        //}

        //TimeSpan _endingTimeOfDay;

        //public TimeSpan ClosingTime
        //{
        //    get { return _endingTimeOfDay; }
        //    set { _endingTimeOfDay = value; }
        //}

        //Stops...

        Dictionary<Location, int> internalStops;

        public IEnumerable<Stop> Stops
        {
            get
            {
                return
                    from kvp in internalStops
                    select new Stop(kvp.Key, kvp.Value);
            }
        }

        public void addToStop(Location stopLocation, int students)
        {
            if (!internalStops.ContainsKey(stopLocation)) internalStops[stopLocation] = students;
            else internalStops[stopLocation] += students;
        }

        public void removeFromStop(Location stopLocation, int students)
        {
            if (internalStops.ContainsKey(stopLocation))
            {
                if (students < internalStops[stopLocation])
                    internalStops[stopLocation] -= students;
                internalStops.Remove(stopLocation);
            }
        }

        public void clearStop(Location stopLocation)
        {
            if (internalStops.ContainsKey(stopLocation))
                internalStops.Remove(stopLocation);
        }

        public School(string name, Location address, IEnumerable<Stop> stops)
        {
            this._address = address;
            this._schoolName = name;
            this.internalStops = new Dictionary<Location, int>();
            foreach (var stop in stops) internalStops.Add(stop, stop.Passengers);
        }

        protected School() { }

        public void relocateStop(Stop toRelocate, Location whereToPutStop, int passengers)
        {
            this.clearStop(toRelocate);
            this.addToStop(whereToPutStop, passengers);
        }

        //note: this is private to control access;
        //the serializer can still access this constructor
        private School(SerializationInfo info, StreamingContext ctxt)
        {
            _address = (Location)(info.GetValue("Address", typeof(Location)));
            _schoolName = info.GetString("Name");
            internalStops = new Dictionary<Location, int>();
            foreach (var kvp
                in (List<KeyValuePair<Location, int>>)info.GetValue
                ("Schools", typeof(List<KeyValuePair<Location, int>>)))
                internalStops.Add(kvp.Key, kvp.Value);
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Address", _address);
            info.AddValue("Name", _schoolName);
            info.AddValue("Schools", internalStops.ToList());
        }
    }
}
